import { createSlice } from '@reduxjs/toolkit';
import { DEFAULT_SETTINGS } from '../../utils/constants';

// 初始状态
const initialState = {
  isActive: false,
  isPaused: false,
  currentPlatform: null,
  settings: DEFAULT_SETTINGS,
  statistics: {
    totalScrolls: 0,
    totalUsageTime: 0,
    dailyStats: {}
  },
  permissions: {
    overlay: false,
    storage: false,
    media: false,
    photo: false,
    background: false
  },
  installedPlatforms: [],
  floatingButtonPosition: { x: 0, y: 0 },
  lastScrollTime: null,
  isLoading: false,
  error: null
};

// 创建slice
const autoScrollSlice = createSlice({
  name: 'autoScroll',
  initialState,
  reducers: {
    // 自动滑动控制
    startAutoScroll: (state, action) => {
      state.isActive = true;
      state.isPaused = false;
      state.currentPlatform = action.payload.platform || state.currentPlatform;
      state.lastScrollTime = Date.now();
      state.error = null;
    },
    
    stopAutoScroll: (state) => {
      state.isActive = false;
      state.isPaused = false;
      state.lastScrollTime = null;
    },
    
    pauseAutoScroll: (state) => {
      state.isPaused = true;
    },
    
    resumeAutoScroll: (state) => {
      state.isPaused = false;
      state.lastScrollTime = Date.now();
    },
    
    // 设置管理
    updateSettings: (state, action) => {
      state.settings = { ...state.settings, ...action.payload };
    },
    
    resetSettings: (state) => {
      state.settings = DEFAULT_SETTINGS;
    },
    
    // 平台管理
    setCurrentPlatform: (state, action) => {
      state.currentPlatform = action.payload;
    },
    
    setInstalledPlatforms: (state, action) => {
      state.installedPlatforms = action.payload;
    },
    
    // 统计更新
    incrementScrollCount: (state) => {
      state.statistics.totalScrolls += 1;
      const today = new Date().toISOString().split('T')[0];
      if (!state.statistics.dailyStats[today]) {
        state.statistics.dailyStats[today] = { scrolls: 0, usageTime: 0 };
      }
      state.statistics.dailyStats[today].scrolls += 1;
    },
    
    addUsageTime: (state, action) => {
      const timeToAdd = action.payload.time || 0;
      state.statistics.totalUsageTime += timeToAdd;
      const today = new Date().toISOString().split('T')[0];
      if (!state.statistics.dailyStats[today]) {
        state.statistics.dailyStats[today] = { scrolls: 0, usageTime: 0 };
      }
      state.statistics.dailyStats[today].usageTime += timeToAdd;
    },
    
    clearStatistics: (state) => {
      state.statistics = {
        totalScrolls: 0,
        totalUsageTime: 0,
        dailyStats: {}
      };
    },
    
    // 权限管理
    setPermission: (state, action) => {
      const { permission, granted } = action.payload;
      state.permissions[permission] = granted;
    },
    
    setPermissions: (state, action) => {
      state.permissions = { ...state.permissions, ...action.payload };
    },
    
    // 悬浮窗位置
    setFloatingButtonPosition: (state, action) => {
      state.floatingButtonPosition = action.payload;
    },
    
    // 加载状态
    setLoading: (state, action) => {
      state.isLoading = action.payload;
    },
    
    // 错误处理
    setError: (state, action) => {
      state.error = action.payload;
    },
    
    clearError: (state) => {
      state.error = null;
    }
  }
});

// 导出actions
export const {
  startAutoScroll,
  stopAutoScroll,
  pauseAutoScroll,
  resumeAutoScroll,
  updateSettings,
  resetSettings,
  setCurrentPlatform,
  setInstalledPlatforms,
  incrementScrollCount,
  addUsageTime,
  clearStatistics,
  setPermission,
  setPermissions,
  setFloatingButtonPosition,
  setLoading,
  setError,
  clearError
} = autoScrollSlice.actions;

// 导出reducer
export default autoScrollSlice.reducer;

// 选择器
export const selectAutoScrollState = (state) => state.autoScroll;
export const selectIsActive = (state) => state.autoScroll.isActive;
export const selectIsPaused = (state) => state.autoScroll.isPaused;
export const selectCurrentPlatform = (state) => state.autoScroll.currentPlatform;
export const selectSettings = (state) => state.autoScroll.settings;
export const selectStatistics = (state) => state.autoScroll.statistics;
export const selectPermissions = (state) => state.autoScroll.permissions;
export const selectInstalledPlatforms = (state) => state.autoScroll.installedPlatforms;
export const selectFloatingButtonPosition = (state) => state.autoScroll.floatingButtonPosition;
export const selectIsLoading = (state) => state.autoScroll.isLoading;
export const selectError = (state) => state.autoScroll.error;